/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**递归法
 * 
 * 1.确定递归函数 返回值 参数
 * 2.确定终止条件
 * 3.确定递归逻辑
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
 var isSameTree = function(p, q) {
     if(p===null&&q===null) return true
     else if(p!==null&&q===null) return false
     else if(p===null&&q!==null) return false
     else if(p.val!==q.val) return false
     return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right) 
};



/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**迭代法
 * 借助队列
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
 var isSameTree = function(p, q) {
    let queue = [p,q]
    while(queue.length){
        let left = queue.shift()
        let right = queue.shift()
        
        if(left==null&&right==null) continue
        else if(left===null&&right!==null) return false
        else if(left!==null&&right===null) return false
        else if(left.val!==right.val) return false
        queue.push(left.left)
        queue.push(right.left)
        queue.push(left.right)
        queue.push(right.right)
    }
    return true
};